# CCNCore::Provision::Application::apache.pm
# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information: http://carrierclass.net/
#
use strict; use warnings;

package CCNCore::Provision::Application::apache;
use base qw(CCNCore::Provision::Base);

use CCN::Text;
use CCNCore::Scripting;
use CCNCore::Home;

sub register
{
    my $self = shift;
    $self->register_on_column('provisioning_servers','server');
}


sub run()
{
    my $self = shift;
    return if $self->p->force_manager;

    $self->PACKAGE ('apache2');
    # $self->PACKAGE ('apache2-mpm-worker');
    $self->PACKAGE ('libapache2-mod-perl2');
    $self->PACKAGE ('libapache-dbi-perl');

    $self->CHECKFOR ('/etc/init.d/apache2');
    $self->CHECKFOR ('/etc/apache2');
    $self->CHECKFOR (CCNCore::Home::www);
    
    # YYYPORT Is /var/log/apache2 Debian-specific?
    $self->CHECKFOR ('/var/log/apache2');

    # Should Apache be enabled?
    $self->disable('apache2'), $self->disable('apache'),
    return
        unless $self->p->is_a_gui_node;

    $self->enable('apache2');

    # TBD Use cgid/mod_perl -- but should be configured by the OS, not by us
    # On Debian etch as of 2006-12 the list of default modules is as follows:
    # alias
    # auth_basic
    # authn_file
    # authz_default
    # authz_groupfile
    # authz_host
    # authz_user
    # autoindex
    # cgid
    # dir
    # env
    # mime
    # negotiation
    # setenvif
    # status
    
    # YYYPORT Is ports.conf Debian-specific?
    foreach('/etc/apache2/ports.conf')
    {
        CAT $_, "Listen 443\n";
        IF_CHANGED $_, '/etc/init.d/apache2 restart';
    }
    
    foreach('/etc/apache2/sites-available/ccn')
    {
        # Make sure our modules can be found by Apache::Registry / mod_perl
        my $libs = join(':',
            CCNCore::Home::www.'/lib',
            CCNCore::Home::manager.'/lib',
        );
        
        my $script = 
            template_text (
            content_of(CCNCore::Home::manager.'/lib/CCNCore/templates/apache-ccn.txt'),
            {
                SYSNAME => $self$self->p->system_name,
                SERVER  => $self->p->server_name,
                LIBS    => $libs,
            } );

        CAT $_, $script;

        # We used to do a reload, but a restart is required if the certificates were changed.
        IF_CHANGED $_, '/etc/init.d/apache2 restart';
        # Note: this is kind of redundant since we do one in any case at the end of this routine.
    }

    if( $self->bootstrapping or $self->upgrading )
    {
        IF_CHANGED '' => '/etc/init.d/apache2 restart';
    }
}

1;